package com.cuitrip.business.home.my.card;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cuitrip.business.home.my.card.presenter.PromotionPresenter;
import com.cuitrip.business.home.my.card.ui.PromotionItemAdapter;
import com.cuitrip.business.pay.model.DiscountItem;
import com.cuitrip.component.RecyclerItemClickListener;
import com.cuitrip.component.item.CtTextView;
import com.cuitrip.component.statelayout.StateLayout;
import com.cuitrip.service.R;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragmentActivity;
import com.umeng.onlineconfig.OnlineConfigAgent;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by sheena on 16/2/27.
 */
public class PromotionActivity extends CustomUiFragmentActivity implements IPromotionView {
    @Bind(R.id.et_promo_code)
    EditText etPromoCode;

    @Bind(R.id.tv_apply)
    TextView tvApply;

    @Bind(R.id.rv_coupons)
    RecyclerView rvCoupons;

    @Bind(R.id.tv_msg)
    CtTextView tvMsg;

    @Bind(R.id.state_layout)
    StateLayout mStateLayout;

    @Bind(R.id.couponBlank)
    LinearLayout llCouponBlank;

    @Bind(R.id.fl_layout)
    FrameLayout flLayout;

    @Bind(R.id.ll_layout)
    LinearLayout llLayout;

    @Bind(R.id.cl_container)
    CoordinatorLayout clContainer;

    private PromotionPresenter mPromotionPresenter;
    private List<DiscountItem> discountItemList;
    private PromotionItemAdapter adapter;

    private static final String SELECT_MODE = "SELECT_MODE";
    private boolean selectMode = true;
    private String promotionValue;


    public static void startActivity(Activity activity, boolean selectMode) {
        Intent intent = new Intent(activity, PromotionActivity.class).putExtra(SELECT_MODE, selectMode);
        activity.startActivityForResult(intent, 1);
    }


    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config = new CustomUiConfig();
        config.mTopbarLeftAreaName = getString(R.string.back_icon);
        config.mTopbarMiddleAreaName = getString(R.string.coupon);
        if (selectMode) {
            config.mRightStyle = CustomUiConfig.AreaStyle.TEXT_STYLE;
            config.mTopbarRightAreaName = getString(R.string.coupon_not_use);
        } else {
            config.mTopbarRightAreaName = "";
        }
        return config;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (getIntent() != null) {
            selectMode = getIntent().getBooleanExtra(SELECT_MODE, false);
        }
        mPromotionPresenter = new PromotionPresenter(this);
        mPromotionPresenter.attachView(this);
        super.onCreate(savedInstanceState, R.layout.activity_promotion);

    }


    @Override
    public void initView() {
        ButterKnife.bind(this);
        promotionValue = OnlineConfigAgent.getInstance().getConfigParams(getApplicationContext(), "promotion_value");
        mPromotionPresenter.renderUi(selectMode);
        mPromotionPresenter.requestData();

    }

    @Override
    public void initListener() {
        etPromoCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                tvApply.setEnabled(s.length() > 0 ? true : false);
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        if (selectMode) {
            rvCoupons.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    DiscountItem discountItem = discountItemList.get(position);
                    Intent intent = new Intent();
                    intent.putExtra("discountItem", discountItem);
                    intent.putExtra("discountMoney", discountItem.getMoney());
                    intent.putExtra("discountId", discountItem.getCode());
                    intent.putExtra("discountMoneyType", discountItem.getMoneyType());
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }));
        }

    }

    @OnClick(R.id.tv_apply)
    public void apply() {
        String code = etPromoCode.getText().toString();
        mPromotionPresenter.checkValidCode(code, clContainer,tvMsg);
    }

    @Override
    public void displayLoading() {

    }

    @Override
    public void displayContent() {
        mStateLayout.changeState(StateLayout.State.INIT);
    }

    @Override
    public void displayEmpty() {
        mStateLayout.changeState(StateLayout.State.Empty);
    }

    @Override
    public void displayNoNetwork() {
        mStateLayout.changeState(StateLayout.State.NoNetwork);
    }

    @Override
    public void applyFailed() {
        //显示错误信息
       // tvMsg.setVisibility(View.VISIBLE);
    }

    @Override
    public void renderCouponList(final List<DiscountItem> discountItems) {
        discountItemList = discountItems;
        boolean isShowPromotion = "1".equals(promotionValue);
        if (isShowPromotion) {
            //开关开
            llLayout.setVisibility(View.VISIBLE);
            flLayout.setVisibility(View.VISIBLE);
        } else {
            llLayout.setVisibility(View.GONE);
            flLayout.setVisibility(View.GONE);
        }
        if (discountItems.size() == 0) {
            llCouponBlank.setVisibility(isShowPromotion?View.GONE:View.VISIBLE);
            rvCoupons.setVisibility(View.GONE);
        }else {
            rvCoupons.setVisibility(View.VISIBLE);
        }
        rvCoupons.setVisibility(View.VISIBLE);
        adapter = new PromotionItemAdapter();
        adapter.setDiscountItems(discountItems);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvCoupons.setLayoutManager(layoutManager);
        rvCoupons.setAdapter(adapter);

    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void onTopbarLeftPress() {
        onBackPressed();
    }

    @Override
    public void onTopbarRightPress() {
        if (selectMode) {
            Intent intent = new Intent();
            DiscountItem discountItem=new DiscountItem();
            discountItem.setCode("");
            discountItem.setMoney("0.00");
            discountItem.setMoneyType("");
            intent.putExtra("discountItem",discountItem);
            setResult(RESULT_OK, intent);
            finish();
        }
    }

}
